#include "global_config.h"
#include "logger.h"
#include <stdlib.h>
#include <string.h>

int global_config_init(global_config_t *config) {
    if (!config) {
        return ERROR_INVALID_PARAM;
    }

    memset(config, 0, sizeof(global_config_t));
    
    // 设置默认值
    config->device_api_ip = strdup("127.0.0.1");
    config->device_api_port = 80;  // 假设默认HTTP端口
    config->http_server_ip = strdup("0.0.0.0");  // 监听所有接口
    config->http_server_port = 8080;
    config->device_poll_interval = 5000;  // 5秒
    config->max_http_clients = 10;
    config->debug_mode = false;
    config->log_level = strdup("info");
    
    return SUCCESS;
}

int global_config_set_device_api(global_config_t *config, const char *ip, int port) {
    if (!config || !ip || port <= 0) {
        return ERROR_INVALID_PARAM;
    }

    if (config->device_api_ip) {
        free(config->device_api_ip);
    }
    config->device_api_ip = strdup(ip);
    config->device_api_port = port;
    
    return SUCCESS;
}

int global_config_set_http_server(global_config_t *config, const char *ip, int port) {
    if (!config || !ip || port <= 0) {
        return ERROR_INVALID_PARAM;
    }

    if (config->http_server_ip) {
        free(config->http_server_ip);
    }
    config->http_server_ip = strdup(ip);
    config->http_server_port = port;
    
    return SUCCESS;
}

const char *global_config_get_device_api_base_url(global_config_t *config) {
    if (!config || !config->device_api_ip) {
        return "http://127.0.0.1";
    }
    
    // 构建基础URL
    static char base_url[256];
    if (config->device_api_port == 80) {
        snprintf(base_url, sizeof(base_url), "http://%s", config->device_api_ip);
    } else {
        snprintf(base_url, sizeof(base_url), "http://%s:%d", config->device_api_ip, config->device_api_port);
    }
    
    return base_url;
}

int global_config_save(global_config_t *config, db_manager_t *db_mgr) {
    if (!config || !db_mgr) {
        return ERROR_INVALID_PARAM;
    }

    json_t *config_json = json_object();
    json_object_set_new(config_json, "device_api_ip", json_string(config->device_api_ip));
    json_object_set_new(config_json, "device_api_port", json_integer(config->device_api_port));
    json_object_set_new(config_json, "http_server_ip", json_string(config->http_server_ip));
    json_object_set_new(config_json, "http_server_port", json_integer(config->http_server_port));
    json_object_set_new(config_json, "device_poll_interval", json_integer(config->device_poll_interval));
    json_object_set_new(config_json, "max_http_clients", json_integer(config->max_http_clients));
    json_object_set_new(config_json, "debug_mode", json_boolean(config->debug_mode));
    json_object_set_new(config_json, "log_level", json_string(config->log_level));

    char *config_str = json_dumps(config_json, JSON_COMPACT);
    json_decref(config_json);

    if (!config_str) {
        return ERROR_JSON_PARSE;
    }

    int result = db_config_set(db_mgr, "global_config", config_str);
    free(config_str);
    
    return result;
}

int global_config_load(global_config_t *config, db_manager_t *db_mgr) {
    if (!config || !db_mgr) {
        return ERROR_INVALID_PARAM;
    }

    char *config_str = db_config_get(db_mgr, "global_config");
    if (!config_str) {
        // 没有保存的配置，使用默认值
        return SUCCESS;
    }

    json_error_t error;
    json_t *config_json = json_loads(config_str, 0, &error);
    free(config_str);

    if (!config_json) {
        LOG_ERROR("Failed to load global config: %s", error.text);
        return ERROR_JSON_PARSE;
    }

    // 解析配置
    json_t *value;
    
    value = json_object_get(config_json, "device_api_ip");
    if (value && json_is_string(value)) {
        if (config->device_api_ip) free(config->device_api_ip);
        config->device_api_ip = strdup(json_string_value(value));
    }
    
    value = json_object_get(config_json, "device_api_port");
    if (value && json_is_integer(value)) {
        config->device_api_port = json_integer_value(value);
    }
    
    value = json_object_get(config_json, "http_server_ip");
    if (value && json_is_string(value)) {
        if (config->http_server_ip) free(config->http_server_ip);
        config->http_server_ip = strdup(json_string_value(value));
    }
    
    value = json_object_get(config_json, "http_server_port");
    if (value && json_is_integer(value)) {
        config->http_server_port = json_integer_value(value);
    }
    
    value = json_object_get(config_json, "device_poll_interval");
    if (value && json_is_integer(value)) {
        config->device_poll_interval = json_integer_value(value);
    }
    
    value = json_object_get(config_json, "max_http_clients");
    if (value && json_is_integer(value)) {
        config->max_http_clients = json_integer_value(value);
    }
    
    value = json_object_get(config_json, "debug_mode");
    if (value && json_is_boolean(value)) {
        config->debug_mode = json_boolean_value(value);
    }
    
    value = json_object_get(config_json, "log_level");
    if (value && json_is_string(value)) {
        if (config->log_level) free(config->log_level);
        config->log_level = strdup(json_string_value(value));
    }

    json_decref(config_json);
    return SUCCESS;
}

void global_config_cleanup(global_config_t *config) {
    if (!config) {
        return;
    }

    if (config->device_api_ip) free(config->device_api_ip);
    if (config->http_server_ip) free(config->http_server_ip);
    if (config->log_level) free(config->log_level);
    
    memset(config, 0, sizeof(global_config_t));
}
